﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iSun.Common;
using System.IO;

namespace iSun.WebApp.AjaxData
{
    public partial class UploadFile : iSun.Web.UI.Controls.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //string[] filext = { "doc","xls","gif", "jpg", "png", "swf","txt","pdf"};
            HttpPostedFile postfile = iSunContext.Request.Files[0];
            if (postfile != null)// && IT9D.Common.Globals.IIFExistExtension(postfile.FileName, filext))
            {
                //if (postfile.ContentLength < 1500 * 1024)
                //{
                string str = string.Format("F" + DateTime.Now.ToString("yyyyMMddHHss") + ".{1}", this.UserName, iSun.Common.Globals.GetExtension(postfile.FileName));
                string kbytes = "0KB";
                //判断是否存在
                if (!Directory.Exists(iSunContext.Request.PhysicalApplicationPath + "upload"))
                {
                    Directory.CreateDirectory(iSunContext.Request.PhysicalApplicationPath + "upload");//创建新路径
                }
                postfile.SaveAs(iSunContext.Request.PhysicalApplicationPath + "upload/" + str);
                if (postfile.ContentLength / 1024<1024)
                {
                    kbytes = Convert.ToString(postfile.ContentLength / 1024) + "KB";
                }
                else
                {
                    kbytes = string.Format("{0:#0.00}",(float)postfile.ContentLength / 1024/1024) + "MB";
                }
                
                string id = this.GetStringFromQueryString("c", "");
                iSunContext.Response.Write(string.Format("<script language='Javascript'>parent.onUpload('{0}','{1}');parent.onUpload('{2}','{3}');parent.onUpload('Size','{3}');parent.onUpload('Caption','" + GetRealFileName(postfile.FileName) + "');parent.closeUpload_div();</script>", id, str, "TrSize", kbytes));
                //}
            }
        }

        /// <summary> 
        /// 功能：获取文件名 
        /// </summary> 
        /// <param name="strPathName">文件路径</param> 
        /// <returns>文件名</returns> 
        private string GetRealFileName(string strPathName) 
        { 
            string [] strFileName = strPathName.Split('\\'); 
            int iPos = strFileName.GetUpperBound(0); 
            return strFileName[iPos]; 
        }
    }
}
